import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_shop.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/commission_list_model.dart';
import 'package:gold/model/shop_commission_model.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:32 2024/12/10

class CommissionDetailsViewmodel extends Viewmodel {
  /// 刷新控制器
  final RefreshController controller = RefreshController();
  /// 数据
  final List<CommissionModel> list = [];

  /// 用户信息
  ShopCommissionModel _shopCommission = ShopCommissionModel();
  ShopCommissionModel get shopCommission => _shopCommission;

  /// 页码
  int _page = 1;

  String _status = CommissionStatus.all;
  /// 提成状态 (全部: '' 0: 待结算, 1: 已结算, 2: 已取消)
  String get status => _status;
  set status(String value) {
    if (_status != value) {
      _status = value;
      loadFirst();
    }
  }

  /// 加载提成统计
  Future _loadStatistics() async {
    final result = await Http().request<ShopCommissionModel>(ApiShop.commission, showErrorMsg: false);
    if (result.data != null) {
      _shopCommission = result.data!;
    }
  }

  /// 首次加载
  Future loadFirst() async {
    setLoading();
    await _loadStatistics();
    _page = 1;
    final result = await Http().request<CommissionListModel>(ApiShop.commissionList,data: {'page': _page, 'status': _status});
    if (result.success) {
      list.clear();
      list.addAll(result.data?.data?? []);
      if (list.isNotEmpty) {
        setSuccess();
        controller.headerMode?.value = RefreshStatus.canRefresh;
        controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      } else {
        setEmpty();
      }
    } else {
      setFailed(msg: result.msg);
    }
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<CommissionListModel>(ApiShop.commissionList,data: {'page': _page, 'status': _status});
    if (result.success) {
      controller.refreshCompleted();
      controller.footerMode?.value = _page < (result.data?.total?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        list.clear();
        list.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<CommissionListModel>(ApiShop.commissionList,data: {'page': _page, 'status': _status});
    if (result.success) {
      controller.loadComplete();
      controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        list.addAll(data);
        setSuccessChange();
      } else {
        controller.loadNoData();
      }
    } else {
      controller.loadFailed();
    }
  }

  @override
  onReload() => loadFirst();

  @override
  onRetry() => loadFirst();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

/// 提成状态 (全部: '' 0: 待结算, 1: 已结算, 2: 已取消)
class CommissionStatus {
  /// '':全部
  static const all = '';
  /// 0:待结算
  static const unsettle = '0';
  /// 1:已结算
  static const settled = '1';
  /// 2:已取消
  static const cancel = '2';
}
